//
//  GRXLoadDataTool.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 16/1/7.
//  Copyright © 2016年 grx. All rights reserved.
//

#import "GRXLoadDataTool.h"
#import "NSDictionary+dictionary.h"
#import "NSString+HTMLExtension.h"
@implementation GRXLoadDataTool


/**
 * 向服务器发送html数据请求,然后把服务器返回的html数据通过block返回
 */
+ (void)requestToServerWithURLString:(NSString *)URLString htmlData:(void(^)(NSString *))htmlData{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        //1.服务器返回的HTML数据
        NSString *htmlString =[NSString stringWithContentsOfURL:[NSURL URLWithString:URLString] encoding:NSUTF8StringEncoding error:nil];
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            htmlData(htmlString);
        });
    });
    
}



+ (void)loadData:(void(^)(NSDictionary *htmlData))htmlData {
    NSString *urlString = @"http://m.mogujie.com/?f=wapbaidusem&ptp=1._mf1_wapbdpz.0.17.eCfWr";
    
    //数据加载
    [GRXLoadDataTool requestToServerWithURLString:urlString htmlData:^(NSString *htmlString) {
        //保存的是字符串
        NSArray *urlArray = [htmlString componentsSeparatedFromString:@"window._firstData = " toString:@"}};"];
        
        NSString *str = [urlArray[0] stringByAppendingString:@"}}"];
        
        NSDictionary *dict = [NSDictionary dictionaryWithJsonString:str];
        
        
        htmlData(dict);

    }];
}

@end
